home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / atbsb002.zip / DEMOTOOL.C < prev    next >
C/C++ Source or Header  |  1993-12-16  |  11KB  |  356 lines

  1. /*
  2.     DEMOTOOL.C -- program to demonstrate the abilities of
  3.         Tool Bar and Status Bar  controls implemented in ESTOOLS.DLL 
  4.     and ERROR.DLL
  5.     Copyright (C) Eugene Sokolov 1992-93, (516)632-7892,
  6.     esokolov@sbchm1.chem.sunysb.edu
  7.         (SUNY at Stony Brook, NY 11794-3400)
  8.  
  9.     You can freely copy, change or redistribute this file as long
  10.     as this notice remains intact.
  11.  
  12.         Last edition 12/16/93 (mm/dd/yy)
  13. */
  14.  
  15. #define STRICT    
  16.  
  17. #include <windows.h>
  18. #include "estools0.h"
  19. #include "esdefs.h"
  20. #include "error0.h"
  21.  
  22.  
  23. #define STATBAR_HEIGHT 22
  24.  
  25. LRESULT CALLBACK     MainWndProc    ( HWND, UINT, WPARAM, LPARAM );
  26.  
  27. HINSTANCE hInst;
  28.  
  29. BOOL InitApplication(HINSTANCE hInstance)
  30. {
  31.    WNDCLASS  wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc     = (long (FAR PASCAL*)())MainWndProc;
  35.                                                         // windows of this class.
  36.    wc.cbClsExtra     = 0;    
  37.    wc.cbWndExtra     = 0;    
  38.    wc.hInstance     = hInstance;
  39.    wc.hIcon         = LoadIcon( hInstance, MAKEINTRESOURCE(TBICON) );
  40.    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground     = GetStockObject(WHITE_BRUSH);
  42.    wc.lpszMenuName     = MAKEINTRESOURCE(TBMENU);
  43.    wc.lpszClassName     = "ESDemoTools";
  44.  
  45.  
  46.    return (RegisterClass(&wc));
  47. }
  48.  
  49.  
  50. /************************************************************************/
  51. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  52. {
  53.     HWND    hWnd;
  54.  
  55.     hInst = hInstance;
  56.  
  57.     hWnd = CreateWindow(
  58.         "ESDemoTools",    // See RegisterClass() call.
  59.         "ES Toolbar Demo",    // Text for window title bar.
  60.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,    // Window style.
  61.         CW_USEDEFAULT,            // Default horizontal position.
  62.         CW_USEDEFAULT,            // Default vertical position.
  63.         CW_USEDEFAULT,            // Default width.
  64.         CW_USEDEFAULT,            // Default height.
  65.         NULL,        
  66.         NULL,        
  67.         hInstance,    
  68.         NULL        
  69.     );
  70.  
  71.     if (!hWnd)
  72.         return (FALSE);
  73.  
  74.     ShowWindow(hWnd, nCmdShow);    // Show the window
  75.     UpdateWindow(hWnd);        // Sends WM_PAINT message
  76.     return (TRUE);        // Returns the value from PostQuitMessage
  77.  
  78. }
  79.  
  80.  
  81. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT message,
  82.                      WPARAM wParam, LPARAM lParam )
  83. {
  84.     static HWND hwndTBar;
  85.     static HWND hwndSBar;
  86.  
  87.     switch (message)
  88.     {
  89.  
  90.     case WM_CREATE:
  91.     {
  92.        POINT pnt;
  93.        RECT    rc;
  94.            GetClientRect( hWnd, &rc );
  95.            pnt.x=pnt.y=0;
  96.        hwndTBar=CreateToolBar( hInst, (LPSTR)MAKEINTRESOURCE(TOOLBAR), hWnd, pnt );
  97.        if( !hwndTBar )
  98.        {
  99.           MessageBox( hWnd, "Could not create Tool Bar window", NULL, MB_OK );
  100.           PostMessage( hWnd, WM_CLOSE, 0, 0L );
  101.        }
  102.        hwndSBar=CreateStatusWindow( WS_CHILD | WS_VISIBLE, 0, rc.bottom-STATBAR_HEIGHT,
  103.              rc.right, STATBAR_HEIGHT, hWnd, hInst );
  104.  
  105.        if( !hwndSBar )
  106.        {
  107.           MessageBox( hWnd, "Could not create Status Bar window", NULL, MB_OK );
  108.           PostMessage( hWnd, WM_CLOSE, 0, 0L );
  109.        }
  110.  
  111.            break;
  112.         }
  113.     case WM_SIZE:
  114.        MoveWindow( hwndSBar, 0, HIWORD(lParam)-STATBAR_HEIGHT,  //Position
  115.                         //the SB window on the
  116.                         //bottom of the parent
  117.           LOWORD(lParam), STATBAR_HEIGHT, TRUE );
  118.        break;
  119.  
  120.     case WM_COMMAND:
  121.  
  122.     /* The following WM_COMMAND message processing was NOT intended to be
  123.        nicely written and serves as an example only to show the abilities
  124.        of the Tool Bar control. */
  125.         
  126.        switch( wParam )
  127.            {
  128.  
  129.           case ID_CMD1:
  130.           case ID_CMD2:
  131.           case ID_CMD3:
  132.           case ID_CMD4:
  133.           case ID_CMD5:
  134.               {
  135.        /* This simply prints the wParam of the WM_COMMAND message
  136.           which is equal to the ID number of the pressed TB button
  137.        */
  138.                  /* Old way: */
  139.              HDC hdc;
  140.                  char buffer[64];
  141.              hdc = GetDC( hWnd );
  142.          wsprintf( buffer,"Message WM_COMMAND, wParam=0x%X (%u), button # %d",
  143.             wParam, wParam, GetButtonNumber( hwndTBar, wParam ) );
  144.              TextOut( hdc, 10, 20, buffer, lstrlen( buffer ) );
  145.          ReleaseDC( hWnd, hdc );
  146.  
  147.          /* New way throught Status Bar, look how much simpler
  148.          it is */
  149.          ErrorHandler( hwndSBar, "Message WM_COMMAND, wParam=0x%X (%u), button # %d",
  150.             wParam, wParam, GetButtonNumber( hwndTBar, wParam ) );
  151.          /* ErrorHandler is an actual export from ERROR.DLL */
  152.          break;
  153.           }
  154.           case 1100:
  155.           case 1200:
  156.           case 1300:
  157.           case 1400:
  158.           case 1500:
  159.           /* Toggle disable/enable button */
  160.           {
  161.          int button=(wParam-1100)/100;
  162.          HMENU hm;
  163.          hm=GetMenu( hWnd );
  164.          CheckMenuItem( hm, wParam,
  165.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  166.                     MF_UNCHECKED:MF_CHECKED );
  167.          ES_TB_Toggle_Enable_Disable_Demo( hwndTBar, button );
  168.          // This function actually calls SendMessage API which sends
  169.          // a message to hwndTBar window to change the corresponding
  170.          // style (explanation and source for these functions is sent to
  171.          // the registred users only).
  172.          // 
  173.          // Of course you can try to 'Spy' the messages. But I do
  174.          // not think the effort would be worth $15. Anyway, you are
  175.          // not allowed to use it in your programs unless you are
  176.          // registered.
  177.           }
  178.           break;
  179.  
  180.           case 1101:
  181.           case 1201:
  182.           case 1301:
  183.           case 1401:
  184.           case 1501:
  185.           /* Toggle standard/auto 2 state button */
  186.           {
  187.          int button=(wParam-1100)/100;
  188.          HMENU hm;
  189.          hm=GetMenu( hWnd );
  190.          CheckMenuItem( hm, wParam,
  191.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  192.                     MF_UNCHECKED:MF_CHECKED );
  193.          ES_TB_Toggle_Standard_Auto2State_Demo( hwndTBar, button );
  194.          // you need to register to get the documentation on this
  195.          // option. Read the explanation above.
  196.           }
  197.           break;
  198.  
  199.           case 1102:
  200.           case 1202:
  201.           case 1302:
  202.           case 1402:
  203.           case 1502:
  204.           /* Toggle standard/2 state button */
  205.           {
  206.          int button=(wParam-1100)/100;
  207.          HMENU hm;
  208.          hm=GetMenu( hWnd );
  209.          CheckMenuItem( hm, wParam,
  210.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  211.                     MF_UNCHECKED:MF_CHECKED );
  212.          ES_TB_Toggle_Standard_2State_Demo( hwndTBar, button );
  213.          // you need to register to get the documentation on this
  214.          // option. Look up there εεε
  215.          break;
  216.           }
  217.           case 2001:
  218.           case 2002:
  219.           case 2003:
  220.           case 2004:
  221.           case 2005:
  222.           /*
  223.          This function calls SendMessage which sends the corresponding
  224.          message to hwndTBar to set the number of buttons per row.
  225.          Although you could specify 0 in resource header to get horizontal
  226.          tool bar, here you have to supply the actual number, 0 does not
  227.          work - the call will be ignored
  228.           */
  229.          ES_TB_Set_Number_of_Controls_Demo( hwndTBar, wParam-2000 ); //Register!
  230.          ShowWindow( hwndTBar, SW_SHOW );
  231.           /*
  232.          Calls to this and following functions will hide the TB from
  233.          the screen. You need to show the window explicitly. I removed
  234.          this call to ShowWindow from DLL to give a programmer the
  235.          possibility to move the window after it's style was changed before
  236.          the window is displayed. Thus you can override the default
  237.                  positions of window and so on.
  238.                */
  239.          break;
  240.                /*
  241.           case 2006:
  242.          I want to point out that there is no example on something
  243.          like
  244.          ES_TB_Toggle_Popup_Child_Demo( hwndTBar );
  245.          It is not because it is undocumented but because it is
  246.          not implemented. This simply does not work, Windows 3.x
  247.          cannot convert WS_POPUP to WS_CHILD and back. Or at least I
  248.          can say I do not know about this possibility. If you want
  249.          this, you need to destroy the TB with one style and create
  250.          with another. Or tell me how to make it work.
  251.             I may include an additionla function to the ESTOOLS.DLL
  252.          to have this work done, but it won't be done throught a SendMessage
  253.                  API but simply by destruction of one window and creation of another.
  254.               */
  255.           case 2007:
  256.           {
  257.                  /* Toggles caption/no caption TB */
  258.          HMENU hm;
  259.          hm=GetMenu( hWnd );
  260.          CheckMenuItem( hm, wParam,
  261.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  262.                     MF_UNCHECKED:MF_CHECKED );
  263.  
  264.          ES_TB_Toggle_Movable_Fixed_Demo( hwndTBar ); //Register!
  265.          ShowWindow( hwndTBar, SW_SHOW );
  266.                  break;
  267.           }
  268.           case 2008:
  269.           {
  270.                  /* Toggles border/no border TB styles */
  271.          HMENU hm;
  272.          hm=GetMenu( hWnd );
  273.          CheckMenuItem( hm, wParam,
  274.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  275.                     MF_UNCHECKED:MF_CHECKED );
  276.  
  277.          ES_TB_Toggle_BorderStyle_Demo( hwndTBar );   //Register!
  278.          ShowWindow( hwndTBar, SW_SHOW );
  279.                  break;
  280.           }
  281.  
  282.           default:;
  283.        }
  284.        break;
  285.     case WM_CLOSE:
  286.        DestroyWindow(hWnd);
  287.        break;
  288.  
  289.     case WM_QUIT:
  290.     case WM_DESTROY:
  291.        PostQuitMessage(0);
  292.        break;
  293.  
  294.     /*
  295.        The following two message control the style of TB caption
  296.        It is intended to be active always when the parent is active.
  297.        It cannot be done without a cooperation from parent's side.
  298.        It may have some bugs. Try, if you have problems, contact me.
  299.     */  
  300.     case WM_NCACTIVATE:
  301.     /* The following is required in order to prevent DefWindowProc
  302.        from redrawing the caption bar of this window in inactive style
  303.        when tool bar window is activated. Try to remove this and you
  304.        will see caption bar flashing when you click in TB window.
  305.        Whenever TB becomes active it passes the active state to it's
  306.        parent and becomes inactive, to look active it redraws it's caption
  307.        in the active style.
  308.  
  309.        **********************************************************
  310.        *************LOOK HERE FOR CHANGES FROM v1.1**************
  311.        **********************************************************
  312.     */
  313.        if( (HWND)LOWORD(lParam)==hwndTBar )
  314.           return DefWindowProc( hWnd, message, TRUE, lParam );
  315.  
  316.     case WM_ACTIVATEAPP:
  317.     /* This is necessary to draw a title bar of TB in inactive style
  318.        when the application becomes inactive. TB is never active and cannot
  319.        process this message by itself. If you remove this part TB will
  320.        remain looking 'active' even if another application is activated */
  321.        if( !wParam )
  322.           SendMessage( hwndTBar, WM_NCACTIVATE, FALSE, 0L );
  323.        else SendMessage( hwndTBar, WM_NCACTIVATE, TRUE, 0L );
  324.        //break;  -- DO NOT PUT IT HERE. Documentation states incorrectly that
  325.        // you can return 0 if you process this message. You cannot, if you
  326.        // return FALSE you prevent another application from activation.
  327.        // If you want you may try to return wParam for default processing.
  328.     default:
  329.        return (DefWindowProc(hWnd, message, wParam, lParam));
  330.    }
  331.    return (NULL);
  332. }
  333.  
  334. #pragma argsused
  335. /**************************************************************/
  336. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  337.              LPSTR lpCmdLine, int nCmdShow)
  338. {
  339.     MSG msg;        
  340.     if( !hPrevInstance )    
  341.        if (!InitApplication(hInstance))
  342.         return (FALSE);    
  343.  
  344.     if (!InitInstance(hInstance, nCmdShow))
  345.        return (FALSE);
  346.  
  347.     while( GetMessage( &msg, NULL, NULL, NULL ) )    
  348.     {
  349.         TranslateMessage(&msg);    
  350.         DispatchMessage(&msg);    
  351.     }
  352.     return (msg.wParam);
  353. }
  354.  
  355.  
  356.